package model

import (
	"ginProject/biz/dao"
	"github.com/shopspring/decimal"
)

type ProductInfo struct {
	ID                int
	Encoding          string
	Name              string
	CustomerId        int
	Price             decimal.Decimal
	AverageCost       decimal.Decimal
	OneCategoryId     int
	TwoCategoryId     int
	CollectStatus     int
	PublishStatus     int
	AuditStatus       int
	ColorType         int
	ProductionDate    string
	RecommendDescript string
	Descript          string
	CreateTime        string
	ModifiedTime      string
}

/**
 * @Author leibidong
 * @Description  根据一级分类id查询所有商品信息
 * @Date  2021/7/5 11:54
 **/
func SelectAllProductById(oneCategoryId int) []ProductInfo {
	db := dao.InitDB()
	var pro []ProductInfo
	db.Raw("select id,encoding,name,publish_status,audit_status,price,average_cost,color_type,"+
		"recommend_descript,descript from product_info where one_category_id=? and "+
		"two_category_id!=0 and customer_id is null ORDER BY modified_time DESC", oneCategoryId).Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 查询所有的商品信息
 * @Date  2021/7/5 12:08
 **/
func SelectAllProduct() []ProductInfo {
	db := dao.InitDB()
	var pro []ProductInfo
	db.Raw("select id,encoding,name,publish_status,audit_status,price,average_cost,color_type," +
		"recommend_descript,descript from product_info where customer_id is null ORDER BY modified_time DESC").Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 根据商品编号和名字查询商品id
 * @Date  2021/7/6 20:01
 **/
func SelectIdByProductName(name, encoding string) ProductInfo {
	db := dao.InitDB()
	var pro ProductInfo
	db.Raw("select id from product_info where name=? and encoding=? and customer_id is null ", name, encoding).Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 根据商品名字查询商品id
 * @Date  2021/7/6 20:09
 **/
func SelectIdByProductName1(name string) ProductInfo {
	db := dao.InitDB()
	var pro ProductInfo
	db.Raw("select id from product_info where name=?  and customer_id is null ", name).Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 根据商品id进行查询
 * @Date  2021/7/5 15:18
 **/
func SelectId(id int) ProductInfo {
	var pro ProductInfo
	db := dao.InitDB()
	db.Raw("select * from product_info where id=? and customer_id is null ", id).Scan(&pro)
	return pro
}

/**
 * @Author leibidong
 * @Description 添加商品信息
 * @Date  2021/7/5 13:56
 **/
func InsertProduct(p ProductInfo) {
	db := dao.InitDB()
	db.Exec("insert into product_info(encoding,name,price,average_cost,one_category_id,two_category_id,"+
		"color_type,production_date,recommend_descript,descript,create_time,modified_time) values(?,?,?,?,?,?,?,?,?,?,?,?)",
		p.Encoding, p.Name, p.Price, p.AverageCost, p.OneCategoryId, p.TwoCategoryId, p.ColorType, p.ProductionDate,
		p.RecommendDescript, p.Descript, p.CreateTime, p.ModifiedTime)
}

/**
 * @Author leibidong
 * @Description 更新商品信息
 * @Date  2021/7/6 18:51
 **/
func UpdateProductMessage(p ProductInfo) {
	db := dao.InitDB()
	db.Exec("update product_info set encoding=? ,name=? , price=? , average_cost=?"+
		" , color_type=? , recommend_descript=? , descript=?   where id =? ", p.Encoding, p.Name, p.Price,
		p.Price, p.ColorType, p.RecommendDescript, p.Descript, p.ID)
}

/**
 * @Author leibidong
 * @Description 删除商品信息
 * @Date  2021/7/6 20:00
 **/
func DeleteProduct(id int) {
	db := dao.InitDB()
	db.Exec("delete from product_info where id=? and customer_id is null", id)
}
